<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter"
                :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="昵称" prop="nickName">
              <el-input v-model="queryParams.nickName" placeholder="请输入昵称" clearable @keyup.enter="handleQuery"/>
            </el-form-item>
            <el-form-item label="微信号" prop="webchat">
              <el-input v-model="queryParams.webchat" placeholder="请输入微信号" clearable @keyup.enter="handleQuery"/>
            </el-form-item>
            <el-form-item label="电话" prop="phone">
              <el-input v-model="queryParams.phone" placeholder="请输入电话" clearable @keyup.enter="handleQuery"/>
            </el-form-item>
            <div style="margin-bottom: 20px;margin-left: 20px">
              <el-radio-group v-model="queryParams.state">
                <el-radio-button label="待审核" value="0" />
                <el-radio-button label="已同意" value="1" />
                <el-radio-button label="已拒绝" value="2" />
              </el-radio-group>
            </div>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <!--          <el-col :span="1.5">-->
          <!--            <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['app:SkDaRenApply:add']">新增</el-button>-->
          <!--          </el-col>-->
          <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()"
                       v-hasPermi="['app:SkDaRenApply:edit']">审核
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()"
                       v-hasPermi="['app:SkDaRenApply:remove']">删除
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport"
                       v-hasPermi="['app:SkDaRenApply:export']">导出
            </el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="SkDaRenApplyList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column label="ID" width="180" align="center" prop="id" v-if="true"/>
        <el-table-column type="expand">
          <template #default="props">
            <div m="4">
              <p m="t-0 b-2">地址: {{ props.row.address }} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;微信号:
                {{ props.row.webchat }}</p>
              <p m="t-0 b-2">个性标签:
                <el-tag style="margin-left: 10px;" v-if="props.row.personalityTag"
                        v-for="(item, index) in props.row.personalityTag.split(',')" type="primary">{{ item }}
                </el-tag>
              </p>
              <p m="t-0 b-2">陪玩类型:
                <el-tag style="margin-left: 10px;" v-if="props.row.accompanyingType"
                        v-for="(item, index) in props.row.accompanyingType.split(',')" type="primary">{{ item }}
                </el-tag>
              </p>
            </div>
          </template>
        </el-table-column>

        <el-table-column label="头像" align="center" prop="headImage" width="100">
          <template #default="scope">
            <image-preview :src="scope.row.headImage" :width="50" :height="50"/>
          </template>
        </el-table-column>
        <el-table-column label="昵称" align="center" prop="nickName"/>
        <el-table-column label="身高" align="center" prop="height"/>
        <el-table-column label="体重" align="center" prop="weight"/>
        <el-table-column label="年龄" align="center" prop="age"/>
        <el-table-column label="性别" align="center" prop="sex"/>
        <el-table-column label="电话" width="120" align="center" prop="phone"/>
        <!--        <el-table-column label="照片" align="center" prop="photo" />-->
        <!--        <el-table-column label="视频" align="center" prop="video" />-->
        <el-table-column label="状态" align="center" prop="state">
          <template #default="scope">
            <el-tag type="primary" v-if="scope.row.state == 0">待审核</el-tag>
            <el-tag type="success" v-if="scope.row.state == 1">已同意</el-tag>
            <el-tag type="warning" v-if="scope.row.state == 2">已拒绝</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" prop="createTime" width="120">
          <template #default="scope">
            <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="审核时间" align="center" prop="updateTime" width="120">
          <template #default="scope">
            <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="所在城市" align="center" prop="city" width="180">
          <template #default="scope">
            <dict-tag  :options="sys_city_title" :value="scope.row.city"/>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
          <template #default="scope">
            <el-tooltip content="审核" placement="top" v-if="scope.row.state==0">
              <el-button link type="primary" icon="Discount" @click="handleUpdate(scope.row)"
                         v-hasPermi="['app:SkDaRenApply:edit']"></el-button>
            </el-tooltip>
            <el-tooltip content="相册/视频" placement="top">
              <el-button link type="primary" icon="MoreFilled" @click="handleUpdate2(scope.row)"
                         v-hasPermi="['app:SkDaRenApply:list']"></el-button>
            </el-tooltip>
            <!--            <el-tooltip content="删除" placement="top">-->
            <!--              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['app:SkDaRenApply:remove']"></el-button>-->
            <!--            </el-tooltip>-->
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
                  v-model:limit="queryParams.pageSize" @pagination="getList"/>
    </el-card>
    <!-- 添加或修改达人申请信息对话框 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="500px" append-to-body>
      <el-form ref="SkDaRenApplyFormRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="状态" prop="state">
          <el-radio-group v-model="form.state">
            <el-radio v-for="dict in shList" :key="dict.value" :value="dict.value">{{ dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <el-dialog title="查看相册和视频" v-model="dialog.visible2" width="500px" append-to-body>
      <div style="font-weight: bold;margin-bottom: 20px">相册</div>
      <div style="display: flex;flex-wrap: wrap">
        <el-image v-for="(item,index) in info.photo.split(',')" :key="index"
                  style="width: 120px; height: 120px;margin-left: 20px;margin-bottom: 10px"
                  :src="item"
                  :zoom-rate="1.2"
                  :max-scale="7"
                  :min-scale="0.2"
                  :preview-src-list="info.photo.split(',')"
                  :initial-index="4"
                  fit="cover"
        />
      </div>
      <div style="font-weight: bold;margin-bottom: 20px;margin-top: 20px">视频</div>
      <video width="440" height="240" controls loop>
        <source :src="info.video" type="video/mp4">
        您的浏览器不支持 video 标签。
      </video>
    </el-dialog>
  </div>
</template>

<script setup name="SkDaRenApply" lang="ts">
import {
  listSkDaRenApply,
  getSkDaRenApply,
  delSkDaRenApply,
  addSkDaRenApply,
  updateSkDaRenApply
} from '@/api/app/SkDaRenApply';
import {SkDaRenApplyVO, SkDaRenApplyQuery, SkDaRenApplyForm} from '@/api/app/SkDaRenApply/types';

const {proxy} = getCurrentInstance() as ComponentInternalInstance;

const {app_daren_state} = toRefs<any>(proxy?.useDict('app_daren_state'));
const { sys_city_title } = toRefs<any>(proxy?.useDict('sys_city_title'));
const shList = ref([
  {
    label:'通过',
    value:1
  },{
    label:'拒绝',
    value:2
  }
])
const SkDaRenApplyList = ref<SkDaRenApplyVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const SkDaRenApplyFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  visible2: false,
  title: ''
});

const initFormData: { personalityTag: undefined; address: undefined; nickName: undefined; headImage: undefined; sex: undefined; accompanyingType: undefined; weight: undefined; photo: undefined; video: undefined; userId: undefined; phone: undefined; id: undefined; state: undefined; age: undefined; webchat: undefined; height: undefined } = {
  id: undefined,
  headImage: undefined,
  nickName: undefined,
  height: undefined,
  weight: undefined,
  age: undefined,
  sex: undefined,
  address: undefined,
  webchat: undefined,
  phone: undefined,
  personalityTag: undefined,
  accompanyingType: undefined,
  photo: undefined,
  video: undefined,
  state: undefined,
  userId: undefined,
}
const data = reactive<PageData<SkDaRenApplyForm, SkDaRenApplyQuery>>({
  form: {...initFormData},
  info: {...initFormData},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    headImage: undefined,
    nickName: undefined,
    height: undefined,
    weight: undefined,
    age: undefined,
    sex: undefined,
    address: undefined,
    webchat: undefined,
    phone: undefined,
    personalityTag: undefined,
    accompanyingType: undefined,
    photo: undefined,
    video: undefined,
    state: 0,
    params: {}
  },
  rules: {
    id: [
      {required: true, message: "ID不能为空", trigger: "blur"}
    ],
    headImage: [
      {required: true, message: "头像不能为空", trigger: "blur"}
    ],
    nickName: [
      {required: true, message: "昵称不能为空", trigger: "blur"}
    ],
    height: [
      {required: true, message: "身高不能为空", trigger: "blur"}
    ],
    weight: [
      {required: true, message: "体重不能为空", trigger: "blur"}
    ],
    age: [
      {required: true, message: "年龄不能为空", trigger: "blur"}
    ],
    sex: [
      {required: true, message: "性别不能为空", trigger: "change"}
    ],
    address: [
      {required: true, message: "地址不能为空", trigger: "blur"}
    ],
    webchat: [
      {required: true, message: "微信号不能为空", trigger: "blur"}
    ],
    personalityTag: [
      {required: true, message: "个性标签不能为空", trigger: "blur"}
    ],
    accompanyingType: [
      {required: true, message: "陪玩类型不能为空", trigger: "change"}
    ],
    photo: [
      {required: true, message: "照片不能为空", trigger: "blur"}
    ],
    video: [
      {required: true, message: "视频不能为空", trigger: "blur"}
    ],
    state: [
      {required: true, message: "状态不能为空", trigger: "blur"}
    ],
  }
});

const {queryParams, form, rules, info} = toRefs(data);

/** 查询达人申请信息列表 */
const getList = async () => {
  loading.value = true;
  const res = await listSkDaRenApply(queryParams.value);
  SkDaRenApplyList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
}

/** 表单重置 */
const reset = () => {
  form.value = {...initFormData};
  SkDaRenApplyFormRef.value?.resetFields();
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  queryParams.value.state = 0;
  handleQuery();
}

/** 多选框选中数据 */
const handleSelectionChange = (selection: SkDaRenApplyVO[]) => {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = "添加达人申请信息";
}

/** 修改按钮操作 */
const handleUpdate = async (row?: SkDaRenApplyVO) => {
  reset();
  const _id = row?.id || ids.value[0]
  const res = await getSkDaRenApply(_id);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = "审核达人申请信息";
}
/** 修改按钮操作 */
const handleUpdate2 = async (row?: SkDaRenApplyVO) => {
  reset();
  const _id = row?.id || ids.value[0]
  const res = await getSkDaRenApply(_id);
  Object.assign(info.value, res.data);
  dialog.visible2 = true;
}

/** 提交按钮 */
const submitForm = () => {
  SkDaRenApplyFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id) {
        await updateSkDaRenApply(form.value).finally(() => buttonLoading.value = false);
      } else {
        await addSkDaRenApply(form.value).finally(() => buttonLoading.value = false);
      }
      proxy?.$modal.msgSuccess("操作成功");
      dialog.visible = false;
      await getList();
    }
  });
}

/** 删除按钮操作 */
const handleDelete = async (row?: SkDaRenApplyVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('是否确认删除达人申请信息编号为"' + _ids + '"的数据项？').finally(() => loading.value = false);
  await delSkDaRenApply(_ids);
  proxy?.$modal.msgSuccess("删除成功");
  await getList();
}

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download('app/SkDaRenApply/export', {
    ...queryParams.value
  }, `SkDaRenApply_${new Date().getTime()}.xlsx`)
}

onMounted(() => {
  getList();
});
</script>
